package com.turing.post.salesmanage.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.Sales;
import com.turing.post.salesmanage.util.SalesBusiness;
import com.turing.post.salesmanage.util.SalesDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 20, 2012
 */
@Stateless(mappedName = "salesmanage.impl.SalesBusinessImpl")
@Remote
@Local
public class SalesBusinessImpl implements SalesBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	
	@EJB(mappedName = "	salesmanage.impl.SalesDaoImpl")
	private SalesDao salesDaoImpl;

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@Override
	public void insertSalesInfo(PCPSWebInput webInput, Sales sales)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "登录用户机构号为NULL");
		}

		// 业务检查
		if (sales == null) {
			throw new SalesManageApplicationException(SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"Sales");
		}
		// 销售商机构号
		if (PCPSUtil.isEmpty(sales.getSaleBranchNo())) {
			throw new SalesManageApplicationException(SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"SaleBranchNo");
		}
		// 名称
		if (PCPSUtil.isEmpty(sales.getSalesName())) {
			throw new SalesManageApplicationException(SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"SalesName");
		}
		// 获取销售员主键
		sales.setSalesId(commonDaoImpl.getUniqueIntegerKey());
		//插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建销售员", "创建销售员，姓名 " + sales.getSalesName());
		// 插入销售员
		salesDaoImpl.insertSales(sales);
	}

	@Override
	public void updateSalesInfo(PCPSWebInput webInput, Sales sales)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "登录用户机构号为NULL");
		}
		Sales _sales = salesDaoImpl.getSalesBySalesNo(sales.getSalesId());
		if (_sales == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "待修改销售员数据库不存在!");
		}
		// 对象赋值
		_sales.setSalesName(sales.getSalesName());
		_sales.setSaleBranchNo(sales.getSaleBranchNo());
		_sales.setLinkAddr(sales.getLinkAddr());
		_sales.setLinkTel(sales.getLinkTel());
		_sales.setRemark(sales.getRemark());
		//插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改销售员", "修改销售员，姓名 " + sales.getSalesName());
		salesDaoImpl.updateSales(_sales);
	}
}